package im.zego.zimexample.ui.room;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import org.json.JSONObject;

import java.util.ArrayList;

import im.zego.zim.ZIM;
import im.zego.zim.callback.ZIMMemberQueriedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMQueryMemberConfig;
import im.zego.zim.entity.ZIMUserInfo;
import im.zego.zim.enums.ZIMConnectionEvent;
import im.zego.zim.enums.ZIMConnectionState;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zimexample.MyBasicActivity;
import im.zego.zimexample.R;
import im.zego.zimexample.SDKManager;
import im.zego.zimexample.entity.UserInfo;
import im.zego.zimexample.ui.room.adapter.MemberListAdapter;


public class ZIMRoomMemberActivity extends MyBasicActivity implements ZIMMemberQueriedCallback {

    private RecyclerView recyclerView;
    private MemberListAdapter memberListAdapter = new MemberListAdapter();
    private String nextFlag = "";
    private String roomID;
    SwipeRefreshLayout swipeRefreshLayout;

    public static void actionStart(Activity mainActivity, String roomID) {
        Intent intent = new Intent(mainActivity, ZIMRoomMemberActivity.class);
        intent.putExtra("roomID", roomID);
        mainActivity.startActivity(intent);

    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        roomID = getIntent().getStringExtra("roomID");
        setContentView(R.layout.activity_room_member);
        recyclerView = findViewById(R.id.relativeLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(memberListAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        swipeRefreshLayout = findViewById(R.id.swipe_view);
        ZIMQueryMemberConfig zimQueryMemberConfig = new ZIMQueryMemberConfig();
        zimQueryMemberConfig.count = 100;
        SDKManager.sharedInstance().queryMember(roomID, zimQueryMemberConfig, ZIMRoomMemberActivity.this);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                ZIMQueryMemberConfig zimQueryMemberConfig = new ZIMQueryMemberConfig();
                zimQueryMemberConfig.nextFlag = nextFlag;
                zimQueryMemberConfig.count = 100;
                SDKManager.sharedInstance().queryMember(roomID, zimQueryMemberConfig, ZIMRoomMemberActivity.this);
            }
        });

        SDKManager.sharedInstance().setRoomMemberLeftCallback(new SDKManager.OnRoomMemberLeftCallback() {
            @Override
            public void onRoomMemberLeft(ZIM zim, ArrayList<ZIMUserInfo> memberList, String roomID) {
                ZIMRoomMemberActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (ZIMUserInfo zimUserInfo : memberList) {
                            memberListAdapter.delete(zimUserInfo.userID);
                        }
                        memberListAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().closeDebugLog(this);
    }

    @Override
    public void onMemberQueried(ArrayList<ZIMUserInfo> arrayList, String nextFlag, ZIMError zimError) {
        swipeRefreshLayout.setRefreshing(false);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                for (ZIMUserInfo info : arrayList) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setSessionID(info.userID);
                    userInfo.setTitle(info.userID);
                    memberListAdapter.addSessionList(userInfo);
                }

                ZIMRoomMemberActivity.this.nextFlag = nextFlag;
                memberListAdapter.notifyDataSetChanged();
            }
        });
    }
}
